package com.qinggeng.project.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.security.LoginUser;
import com.qinggeng.project.api.dto.Classify;
import com.qinggeng.project.api.dto.GGOrder;
import com.qinggeng.project.api.dto.GGOrderList;
import com.qinggeng.project.api.mapper.GGZYMapper;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.api.service.IGGZYService;
import com.qinggeng.project.system.domain.*;
import com.qinggeng.project.system.domain.vo.OrdersVO;
import com.qinggeng.project.system.dto.PointOrderDTO;
import com.qinggeng.project.system.mapper.OrdersGoodsMapper;
import com.qinggeng.project.system.mapper.OrdersMapper;
import com.qinggeng.project.system.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class IGGZYServiceImpl implements IGGZYService {

    @Resource
    private GGZYMapper ggzyMapper;

    @Resource
    private OrdersGoodsMapper ordersGoodsMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<GGOrderList> getGGOrderList(Date startTime, Date endTime, Long pointId, Long classifyId) {
        List<GGOrderList>  list = new ArrayList<GGOrderList>();
        List<Orders> orders = ggzyMapper.selectOrdersByPointId(pointId,startTime,endTime,classifyId);
        for(Orders order : orders){
            BigDecimal totalPrice =BigDecimal.ZERO;
            GGOrderList dto = new GGOrderList();
                dto.setOrder(order);
            List<GGOrder>  orderList = ggzyMapper.statisticsOfGoods(order.getOrdersId(),classifyId);
                dto.setList(orderList);
                for(GGOrder p:orderList){
                    totalPrice =totalPrice.add(p.getTotalPercentage());
                }
            dto.setTotalPrice(totalPrice);
            list.add(dto);
        }
        return list;
    }

    @Override
    public List<GGOrderList> getReGGOrderList(Date startTime, Date endTime, Long pointId, Long classifyId) {
        List<GGOrderList>  list = new ArrayList<GGOrderList>();
        List<Orders> orders = ggzyMapper.selectReOrdersByPointId(pointId,startTime,endTime,classifyId);
        for(Orders order : orders){
            BigDecimal totalPrice =BigDecimal.ZERO;
            GGOrderList dto = new GGOrderList();
            dto.setOrder(order);
            List<GGOrder>  orderList = ggzyMapper.reStatisticsOfGoods(order.getOrdersId());
            dto.setList(orderList);
            for(GGOrder p:orderList){
                totalPrice =totalPrice.add(p.getTotalPercentage());
            }
            dto.setTotalPrice(totalPrice);
            list.add(dto);
        }
        return list;
    }

    @Override
    public Long selectPointIdByUserId(Long userId) {
        return ggzyMapper.selectPointIdByUserId(userId);
    }

    @Override
    public List<Classify> getClassifyList() {
        return ggzyMapper.getClassifyList();
    }

    @Override
    public List<HistoryWithdraw> getWithDrawCashList(Long userId) {
        return ggzyMapper.getWithDrawCashList(userId);
    }

    @Override
    public List<OrdersVO> listOrderVO(PointOrderDTO dto) {
        LoginUser loginUser = ApiUtils.getApiUser();
        if (loginUser==null){
            throw  new CustomException(HttpStatus.UNAUTHORIZED.value(),"当前用户信息失效");
        }
        SysUser sysUser=loginUser.getUser();
        if (sysUser==null){
            throw  new CustomException(HttpStatus.UNAUTHORIZED.value(),"当前用户信息失效");
        }
        String phonenumber=sysUser.getPhonenumber();
        if (StringUtils.isEmpty(phonenumber)){
            throw  new CustomException(HttpStatus.UNAUTHORIZED.value(),"当前用户信息失效");
        }
        Long userId=sysUserMapper.selectUserIdByPhoneNumberAndUserType(phonenumber, UserTypeEnum.COURIER.getCode());
        if (null==userId) {
            throw  new CustomException("该用户信息没有团长身份");
        }
        Long pointId =selectPointIdByUserId(userId);
        dto.setPointdId(pointId);
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        String startTime=dto.getStartTime();
        if (StringUtils.isNotEmpty(startTime)){
            dto.setStartTime(startTime+" 00:00:00");
        }
        String endTime=dto.getEndTime();
        if (StringUtils.isNotEmpty(endTime)){
            dto.setEndTime(endTime+" 59:59:59");
        }
        List<OrdersVO> ordersVOS= ordersGoodsMapper.listPointOrder(dto);
        if (null==ordersVOS||ordersVOS.isEmpty()){
            return  new ArrayList<>();
        }
        for (OrdersVO ordersVO : ordersVOS) {
           String fee= ordersVO.getDistributionFee();
           if (StringUtils.isNotEmpty(fee)){
               if (new BigDecimal(fee).compareTo(BigDecimal.ZERO)>=0){
                  List<OrdersGoods> goodsList= ordersVO.getOrdersGoods();
                   OrdersGoods ordersGoods=new OrdersGoods();
                   ordersGoods.setAttributes("配送费");
                   ordersGoods.setGoodsName("配送费");
                   ordersGoods.setGoodsPrice(new BigDecimal(fee));
                   ordersGoods.setGoodsNum(1);
                   ordersGoods.setPercentage(new BigDecimal(fee));
                   goodsList.add(ordersGoods);
               }
           }

        }
        return  ordersVOS;
    }

}
